<?php
class NoticeController extends Zend_Controller_Action
{
	
    public function init()
    {
    	Zend_Loader::loadClass('LoginDefaultModel');
    	Zend_Loader::loadClass('SubjectsModel');
    	Zend_Loader::loadClass('TeacherModel');
    	Zend_Loader::loadClass('ExercisesModel');
    	Zend_Loader::loadClass('Zend_Session');
    	Zend_Loader::loadClass('Zend_Session_Namespace');
        Zend_Loader::loadClass('PointModel');
        Zend_Loader::loadClass('StudentsModel');
        Zend_Loader::loadClass('NoticeModel');
    }
    
    public function preDispatch(){
    	
    	/*
    	 * Load template
    	*/
    	$layoutPath = APPLICATION_PATH  . '/templates/default/default';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    	$lgNamespace = new Zend_Session_Namespace('enrol_teacher');
        $auth = Zend_Auth::getInstance();
    	if(($lgNamespace->enrol_teacher !=1) || !($auth->hasIdentity())){
    		echo "Not enrol";die;
    	}
    }

    function addNoticeAction(){
        
        if($this->_request->isPost()){
            /*
             * Nhan du lieu dau vao
             */
            $notice["title"] = $this->_request->getParam('title');
            $notice["content"] = $this->_request->getParam('content');
            $notice["id_sj"] = $this->_request->getParam('id_sj');;
            $notice["link"] = "";

            $auth = Zend_Auth::getInstance();
            $user = $auth->getIdentity();
            $notice["id_teacher"] = $user->id;

            if($notice["title"] && $notice["content"] && $notice["id_sj"]){
                /*
                 * Xu ly upload file.
                 */
                $error_upload = 0;
                if(($_FILES["file"]["name"])){
                    $notice["link"] = ExercisesModel::uploadFile();
                    if (!preg_match('/\.(rar|zip)$/i',$notice["link"])) {
                        $error_upload = 1;
                    }
                }
                if(!$error_upload){
                    NoticeModel::add($notice);
                    $this->view->success = 1;
                    $this->view->error_upload = $error_upload;
                }
                else{
                    $this->view->warning = 1;
                }
            //  Zend_Debug::dump($notice["link"]);die;
            }
            else{
                $this->view->warning = 1;
            }
            $this->view->notice = $notice;
        }
        // Tra ve danh sach mon hoc ma giao vien day trong ky hien tai
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();
        $listSuject = SubjectsModel::getSubjectOfTeacher($user->id);
      //  Zend_Debug::dump($listSuject);die;
        $this->view->list = $listSuject;
        
    }

    function editNoticeAction(){

        if($this->_request->isPost()){
            /*
             * Nhan du lieu dau vao
             */
            $notice["title"] = $this->_request->getParam('title');
            $notice["content"] = $this->_request->getParam('content');
            $notice["link"] = $this->_request->getParam('file_old');
            $notice["id"] = $this->_request->getParam('id_notice');
            $notice["id_sj"] = $this->_request->getParam('id_sj');
            $auth = Zend_Auth::getInstance();
            $user = $auth->getIdentity();
            $notice["id_teacher"] = $user->id;
            
          //  Zend_Debug::dump($notice);die;
            if($notice["title"] && $notice["content"] && $notice["id_sj"]){
                /*
                 * Xu ly upload file.
                 */

                $error_upload = 0;
                if(($_FILES["file"]["name"])){

                    $notice["link"] = ExercisesModel::uploadFile();
                 //   echo $notice["link"];die;
                    if (!preg_match('/\.(rar|zip)$/i',$notice["link"])) {
                        $error_upload = 1;
                    }
                }
                if(!$error_upload){
                 //   echo 12;die;
                    NoticeModel::edit($notice);
                    $this->view->success = 1;
                    $this->view->error_upload = $error_upload;

                }
                else{
                    $this->view->warning = 1;
                }
            //  Zend_Debug::dump($notice["link"]);die;
            }
            else{
                $this->view->warning = 1;
            //    echo $notice["id_sj"]."  id_sj";die;
              //  echo "thoeu dl";die;
            }
            $this->view->notice = $notice;
        }

        $dt = new NoticeModel();
        $request = $this->getRequest();
        $id = $request->getParam('id');
        $data = $dt->getNoticeById($id);
        $this->view->data = $data;

        // Tra ve danh sach mon hoc ma giao vien day trong ky hien tai
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();
        $listSuject = SubjectsModel::getSubjectOfTeacher($user->id);
       // Zend_Debug::dump($listSuject);die;
        $this->view->list = $listSuject;

    }

}